<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_Model_Category extends Core_Model_Item_Abstract {
	// Properties
	protected $_searchColumns = array ();
	
	// General
	public function getHref($params = array()) {
		$params = array_merge ( array ('route' => 'cmspage_browse', 'reset' => true, 'category' => $this->category_id ), $params );
		$route = $params ['route'];
		$reset = $params ['reset'];
		unset ( $params ['route'] );
		unset ( $params ['reset'] );
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( $params, $route, $reset );
	}
	
	public function getTitle() {
		return $this->category_name;
	}
	
	public function getTable() {
		if (is_null ( $this->_table )) {
			$this->_table = Engine_Api::_ ()->getDbtable ( 'categories', 'cmspage' );
		}
		
		return $this->_table;
	}
	
	public function getUsedCount() {
		$table = Engine_Api::_ ()->getDbTable ( 'cmspages', 'cmspage' );
		$rName = $table->info ( 'name' );
		$select = $table->select ()->from ( $rName )->where ( $rName . '.category_id = ?', $this->category_id );
		$row = $table->fetchAll ( $select );
		$total = count ( $row );
		return $total;
	}
	
	public function setPhoto($photo) {
		if ($photo instanceof Zend_Form_Element_File) {
			$file = $photo->getFileName ();
		} else if ($photo instanceof Storage_Model_File) {
			$file = $photo->temporary ();
		} else if ($photo instanceof Core_Model_Item_Abstract && ! empty ( $photo->file_id )) {
			$file = Engine_Api::_ ()->getItem ( 'storage_file', $photo->file_id )->temporary ();
		} else if (is_array ( $photo ) && ! empty ( $photo ['tmp_name'] )) {
			$file = $photo ['tmp_name'];
		} else if (is_string ( $photo ) && file_exists ( $photo )) {
			$file = $photo;
		} else {
			throw new Article_Model_Exception ( 'invalid argument passed to setPhoto' );
		}
		
		$name = basename ( $file );
		$path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
		$params = array ('parent_type' => $this->getType (), 'parent_id' => $this->getIdentity () );
		
		// Save
		$storage = Engine_Api::_ ()->storage ();
		
		// Resize image (main)
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 720, 720 )->write ( $path . '/m_' . $name )->destroy ();
		
		// Resize image (profile)
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 200, 400 )->write ( $path . '/p_' . $name )->destroy ();
		
		// Resize image (normal)
		$image = Engine_Image::factory ();
		$image->open ( $file )->resize ( 140, 160 )->write ( $path . '/in_' . $name )->destroy ();
		
		// Resize image (icon)
		$image = Engine_Image::factory ();
		$image->open ( $file );
		
		$size = min ( $image->height, $image->width );
		$x = ($image->width - $size) / 2;
		$y = ($image->height - $size) / 2;
		
		$image->resample ( $x, $y, $size, $size, 48, 48 )->write ( $path . '/is_' . $name )->destroy ();
		
		// Resize image (mini)
		$image = Engine_Image::factory ();
		$image->open ( $file );
		
		$size = min ( $image->height, $image->width );
		$x = ($image->width - $size) / 2;
		$y = ($image->height - $size) / 2;
		
		$image->resample ( $x, $y, $size, $size, 16, 16 )->write ( $path . '/imn_' . $name )->destroy ();
		
		// Store
		$iMain = $storage->create ( $path . '/m_' . $name, $params );
		$iProfile = $storage->create ( $path . '/p_' . $name, $params );
		$iIconNormal = $storage->create ( $path . '/in_' . $name, $params );
		$iSquare = $storage->create ( $path . '/is_' . $name, $params );
		$iMini = $storage->create ( $path . '/imn_' . $name, $params );
		
		$iMain->bridge ( $iProfile, 'thumb.profile' );
		$iMain->bridge ( $iIconNormal, 'thumb.normal' );
		$iMain->bridge ( $iSquare, 'thumb.icon' );
		$iMain->bridge ( $iMini, 'thumb.mini' );
		
		// Remove temp files
		@unlink ( $path . '/p_' . $name );
		@unlink ( $path . '/m_' . $name );
		@unlink ( $path . '/in_' . $name );
		@unlink ( $path . '/is_' . $name );
		
		// Update row
		$this->photo_id = $iMain->file_id;
		$this->save ();
		
		return $this;
	}

}